/**
 * 
 */
package com.jeedle.nahe;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
//import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;


/**
 * @author maximo
 *
 */



public class SaveFileFragment extends DialogFragment {
	
	private String m_chosenDir = ".";
    private boolean m_newFolderEnabled = true;
	 
 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
 {
	 	AlertDialog.Builder build = new AlertDialog.Builder(getActivity());
	 	LayoutInflater inflater = getActivity().getLayoutInflater();
	 	
	 	build.setView(inflater.inflate(R.layout.dialog_save_file, null));
	 	build.setMessage(R.string.SaveMessage).	 	
	 	 setPositiveButton(R.string.DialogSave, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				final Context context = getActivity().getApplicationContext();

                
    			final EditText filename = (EditText) getDialog().findViewById(R.id.editFilename);
				final MultiAutoCompleteTextView htmlCode = (MultiAutoCompleteTextView) getActivity().findViewById(R.id.multiAutoCompleteTextView1);
			    //Context context = getActivity().getApplicationContext();
			    
			    
				 // Create DirectoryChooserDialog and register a callback 
                DirectoryChooserDialog directoryChooserDialog = 
                new DirectoryChooserDialog(getActivity(), 
                    new DirectoryChooserDialog.ChosenDirectoryListener() 
                {
                    @Override
                    public void onChosenDir(String chosenDir) 
                    {
                        m_chosenDir = chosenDir;
                        Toast.makeText(
                        		context, "Chosen directory: " + 
                          chosenDir, Toast.LENGTH_LONG).show();
                        
                        String filepath = m_chosenDir + "/" + filename.getText().toString();
         			    
                        try
        			    {  
        			    	File file =  new File(filepath);
        			    	file.createNewFile();
        				    FileOutputStream fileout = new FileOutputStream(file, false);
        				    fileout.write(htmlCode.getText().toString().getBytes());
        				    fileout.close();
        			    	
        			    }
        			    catch (IOException e)
        			    {
        			    	e.printStackTrace();
        			    }   
                    }
                }); 
                
                // Toggle new folder button enabling
                directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
                // Load directory chooser dialog for initial 'm_chosenDir' directory.
                // The registered callback will be called upon final directory selection.
                directoryChooserDialog.chooseDirectory(m_chosenDir);
                m_newFolderEnabled = ! m_newFolderEnabled;
                
			}
		}).
		setNegativeButton(R.string.CancelString, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				SaveFileFragment.this.getDialog().cancel();
			}
		});
	
  return build.create();
 }
}




